package game.components;

import game.fury.FuryScreen;
import sheep.game.Sprite;
import sheep.game.State;
import sheep.math.Vector2;

/**
 *	Updates balls, padle and powerUps. 
 * @author Erlend Heimark
 */

public class Update extends State {
	
	/**
	 * Adds a new ball when the last ball on the play field is lost
	 * @param furyScreen The game object
	 */
	public static void resetBall(FuryScreen furyScreen){
		Balls temp;
		
		if(Score.getBalls() == 2){
			temp = new Balls(furyScreen.con.ballimage,new Vector2(-130, 150), new Vector2(240, 300), furyScreen.con.ballStrengthStd);
		}
		if(Score.getBalls() == 1){
			temp = new Balls(furyScreen.con.ballimage,new Vector2(140, 160), new Vector2(180, 300), furyScreen.con.ballStrengthStd);
		}
		else{
			temp = new Balls(furyScreen.con.ballimage,new Vector2(-140, 160), new Vector2(180, 300), furyScreen.con.ballStrengthStd);
		}
		
		furyScreen.balls.add(temp);
		furyScreen.addToColLayer(temp);

	}
	
	/**
	 * Pauses balls and powerUps
	 * @param furyScreen The game object
	 */
	public static  void pauseGame(FuryScreen furyScreen) {
		
		for(int i = 0; i<furyScreen.balls.size(); i++){
			furyScreen.balls.get(i).setSpeed(0, 0);
		}
		
		for(int i = 0; i<furyScreen.powerUps.size(); i++){
			furyScreen.powerUps.get(i).setSpeed(0, 0);
		}
		
	}
	
	
	/**
	 * Sets the balls in motion when the game first starts, using default speed
	 * @param furyScreen The game object containing balls and constants 
	 */
	public static void startBalls(FuryScreen furyScreen) {
		
		for(int i = 0; i<furyScreen.balls.size(); i++){
			furyScreen.balls.get(i).setSpeed(furyScreen.con.ballSpeed);
		}
		
	}
	
	/**
	 * Sets a new padle position 
	 * @param x The x position of the touch event 
	 * @param padleSprite The padle sprite being moved
	 */
	public static void movePadle(float x, Sprite padleSprite) {

		padleSprite.setPosition(x, padleSprite.getPosition().getY());
		
	}
	
	/**
	 * Adds a powerUp to the playing field at the position given, and to the CollisionLayer 
	 * @param position The position the powerUp will be drawn
	 * @param furyScreen The Game object containing Constants and Arraylists
	 * @author Marius Haugen
	 */
	public static void addPowerUp(Vector2 position, FuryScreen furyScreen) {
		String power = "";
		Vector2 tempPos = position;
		double rand = Math.random();
		PowerUp temp = null;
		
		if(rand<0.2) {
			power = furyScreen.con.padlePowerBig;
			temp = new PowerUp(furyScreen.con.bluePowerup,tempPos,furyScreen.con.powerUpSpeed,power);
		}
		else if (rand>0.2 & rand <0.5){
			power = furyScreen.con.padlePowerSmall;
			temp = new PowerUp(furyScreen.con.redPowerup,tempPos,furyScreen.con.powerUpSpeed,power);
		}
		else if(rand>0.5 & rand<0.7){
			power = furyScreen.con.ballPowerStrong;
			temp = new PowerUp(furyScreen.con.yellowPowerup,tempPos,furyScreen.con.powerUpSpeed,power);
		}
		else if(rand>0.99){
			power = furyScreen.con.ballPowerChuck;
			temp = new PowerUp(furyScreen.con.chuck, tempPos, furyScreen.con.powerUpSpeed, power);
		}
		else{
			power = furyScreen.con.padlePowerNorm;
			temp = new PowerUp(furyScreen.con.greenPowerup,tempPos,furyScreen.con.powerUpSpeed,power);
		}
		
		
		
		furyScreen.powerUps.add(temp);
		furyScreen.addToColLayer(temp);
	}

	
	/**
	 * Sets a new color on a brick
	 * @param brick The brick being changed
	 * @param furyScreen The game object containing Constants and Arraylists
	 */
	public static void setColor(Brick brick, FuryScreen furyScreen){
		Brick nyBrick;
		furyScreen.removeFromColLayer(brick);
		furyScreen.bricks.remove(brick);
		int i = brick.getBrickCount();
		
		if(i == 0){
			nyBrick = new Brick(furyScreen.con.brickimageRed,brick.getPosition(), i, furyScreen.con.brickFreq);
			nyBrick.setHasPowerUp(brick.getPowerUp());
			furyScreen.bricks.add(nyBrick);
			furyScreen.addToColLayer(nyBrick);
		}
		else if(i == 1){
			nyBrick = new Brick(furyScreen.con.brickimageBlue,brick.getPosition(), i, furyScreen.con.brickFreq);
			nyBrick.setHasPowerUp(brick.getPowerUp());
			furyScreen.bricks.add(nyBrick);
			furyScreen.addToColLayer(nyBrick);
		}
		else if(i == 2){
			nyBrick = new Brick(furyScreen.con.brickimageGreen,brick.getPosition(), i, furyScreen.con.brickFreq);
			nyBrick.setHasPowerUp(brick.getPowerUp());
			furyScreen.bricks.add(nyBrick);
			furyScreen.addToColLayer(nyBrick);
		}
	}

	/**
	 * Uses a powerUp
	 * @param powerUp The powerup being used
	 * @param furyScreen The game object containing Constants and Arraylists 
	 */
	public static void usePowerUp(PowerUp powerUp,FuryScreen furyScreen) {
	
		
		if(powerUp.getPower().equals(furyScreen.con.padlePowerSmall)) {
			furyScreen.colLayer.removeSprite(furyScreen.padle);
			furyScreen.padle = new Padle(furyScreen.con.padleimage_small, furyScreen.padle.getPosition(), furyScreen.con.padleSizeSmall);
			furyScreen.colLayer.addSprite(furyScreen.padle);
			Score.levelScore = Score.levelScore + 3;
		}
		if(powerUp.getPower().equals(furyScreen.con.padlePowerNorm)){
			furyScreen.colLayer.removeSprite(furyScreen.padle);
			furyScreen.padle = new Padle(furyScreen.con.padleimage_std, furyScreen.padle.getPosition(), furyScreen.con.padleSizeDefault); 
			furyScreen.colLayer.addSprite(furyScreen.padle);
			Score.levelScore = Score.levelScore + 2;
		}
		if(powerUp.getPower().equals(furyScreen.con.padlePowerBig)){
			furyScreen.colLayer.removeSprite(furyScreen.padle);
			furyScreen.padle = new Padle(furyScreen.con.padleimage_big, furyScreen.padle.getPosition(), furyScreen.con.padleSizeBig);
			furyScreen.colLayer.addSprite(furyScreen.padle);
			Score.levelScore = Score.levelScore + 1;
		}
		if(powerUp.getPower().equals(furyScreen.con.ballPowerStrong)){
			Balls temp = new Balls(furyScreen.con.ballImageYellow,new Vector2(140, 160), new Vector2(180, 300),furyScreen.con.ballStrengthYellow);
			furyScreen.balls.add(temp);
			furyScreen.addToColLayer(temp);
			Score.levelScore = Score.levelScore + 3;
		}
		if(powerUp.getPower().equals(furyScreen.con.ballPowerChuck)){
			Balls temp = new Balls(furyScreen.con.ballImageChuck,new Vector2(170, 170), new Vector2(180, 300),furyScreen.con.ballStrengthChuck);
			temp.setChuck(true);
			furyScreen.balls.add(temp);
			furyScreen.addToColLayer(temp);
			Score.levelScore = Score.levelScore + 20;
		}
		
	}
	
	/**
	 * Checks if the balls has hit the wall, and turns them around. Updates balls and powerUps  
	 * @param furyScreen The game object containing Constants and Arraylists
	 * @param dt
	 */
	public static void updateFuryScreen(FuryScreen furyScreen, float dt) {
		// check ballz
		for(int i=0; i<furyScreen.balls.size(); i++){
			
			Balls ball = furyScreen.balls.get(i);
			Vector2 speed = ball.getSpeed();
			Vector2 pos = ball.getPosition();
			 
			
			if(pos.getX() > furyScreen.con.rigthbound){
				ball.setSpeed(-(speed.getX()), speed.getY());
				ball.setPosition((pos.getX()-5), pos.getY());
			}
			
			if(pos.getX() < furyScreen.con.leftbound){
				ball.setSpeed(-(speed.getX()), speed.getY());
				ball.setPosition((pos.getX()+5), pos.getY());
			}
			
			if(pos.getY() < furyScreen.con.upperbound){
				ball.setSpeed(speed.getX(), (-speed.getY()));
				ball.setPosition(pos.getX(), (pos.getY()+5));
			}
			
			if(pos.getY() > furyScreen.con.lowerbound){

				furyScreen.removeFromColLayer(ball);
				furyScreen.balls.remove(ball);
				
				if(Score.ball>0){
					Score.ball--;
					if(furyScreen.balls.size() == 0){
						resetBall(furyScreen);
					}
	
				}
				else if(Score.ball == 0 & furyScreen.balls.size() == 0){
					furyScreen.gameOver = true;
					
				}
			}
			
		}
		
		// update ballz
		for(int i=0; i<furyScreen.balls.size(); i++){
			furyScreen.balls.get(i).update(dt);
		}
		
		// update powerups
		for(int i =0; i<furyScreen.powerUps.size(); i++){
			furyScreen.powerUps.get(i).update(dt);
		}

	}

}
